package com.yf.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.basic.Utils;
import com.yf.dto.Article;
import com.yf.entity.CmsContent;
import com.yf.entity.CmsContentExample;
import com.yf.entity.CmsContentFile;
import com.yf.entity.CmsContentFileExample;
import com.yf.entity.SysUser;
import com.yf.mapper.CmsContentFileMapper;
import com.yf.mapper.CmsContentMapper;
import com.yf.service.ArticleService;

@Service
@Transactional
public class ArticleServiceImpl implements ArticleService {

	@Autowired
	private CmsContentMapper cmsContentMapper;
	@Autowired
	private CmsContentFileMapper cmsContentFileMapper;
	
	@Override
	public void addArticle(Map<String,Object> articleMap) {
		CmsContent cmsContent=new CmsContent();
		cmsContent.setContent(articleMap.get("content").toString());	
		cmsContent.setTitle(articleMap.get("title").toString());
		SysUser user=(SysUser)articleMap.get("user");
		cmsContent.setUserId(user.getId());
		cmsContent.setCategoryId((Integer)articleMap.get("departmentId"));
		Date date = new Date();
		cmsContent.setPublishDate(date);
		//获取文章中图片链接地址
		cmsContent.setHasImages(true);
		List<String> imgUrlList=Utils.getUploadUrl(Utils.imgUrlRegex,cmsContent.getContent());
		if(imgUrlList.isEmpty()) 
			cmsContent.setHasImages(false);
		//获取文章中的附件地址
		cmsContent.setHasFiles(true);
		List<String> attachmentUrlList=Utils.getUploadUrl(Utils.attachmentUrlRegex,cmsContent.getContent());
		if(attachmentUrlList.isEmpty()) 
			cmsContent.setHasFiles(false);
		//添加文章
		cmsContentMapper.insertSelective(cmsContent);
		//将图片地址添加到CmsContentFile表中
		if(cmsContent.getHasImages()) {
			CmsContentFile contentFile=new CmsContentFile();
			contentFile.setContentId(cmsContent.getId());
			contentFile.setIsImage(true);
			imgUrlList.forEach(url->{
				contentFile.setFilePath(url);
				cmsContentFileMapper.insertSelective(contentFile);
			});
		}
		//向数据库插入附件地址
		if(cmsContent.getHasFiles()) {
			CmsContentFile contentFile=new CmsContentFile();
			contentFile.setContentId(cmsContent.getId());
			contentFile.setIsImage(false);
			attachmentUrlList.forEach(url->{
				contentFile.setFilePath(url);
				cmsContentFileMapper.insertSelective(contentFile);
			});
		}
	}

	@Override
	public PageInfo<CmsContent> getArticle(Integer depatId,Integer pn,Integer pz) {
		CmsContentExample example=new CmsContentExample();
		//设置按发布日期排序
		example.setOrderByClause("publish_date DESC");
		//设置查询条件为部门id
		example.createCriteria().andCategoryIdEqualTo(depatId);
		//使用pagehelper进行分页查询
		PageHelper.startPage(pn,pz);
		List<CmsContent> contentList=cmsContentMapper.selectByExampleWithBLOBs(example);
		//将查询到的文章内容转换成纯文本
		contentList.forEach(item->{
			String htmlText=item.getContent();
			String text=Utils.html2Text(htmlText);
			item.setContent(text);
		});
		//将查询结果包装成pageInfo对象，此对象内包含详细的分页信息，并设置导航页数
		PageInfo<CmsContent> pageInfo=new PageInfo<CmsContent>(contentList,5);
		//查询部门信息
		return pageInfo;
	}

	@Override
	public CmsContent getArticleById(Long id) {
		CmsContent content=cmsContentMapper.selectByPrimaryKey(id);
		return content;
	}

	@Override
	public void updateArticle(CmsContent content) {
		//从cmsContentFile表中删除该文章Id信息
		CmsContentFileExample fileExample=new CmsContentFileExample();
		fileExample.createCriteria().andContentIdEqualTo(content.getId());
		cmsContentFileMapper.deleteByExample(fileExample);
		//获取文章中的图片地址
		List<String> imgUrlList=Utils.getUploadUrl(Utils.imgUrlRegex,content.getContent());
		content.setHasImages(true);
		//获取文章中的附件地址	
		List<String> attachmentUrlList=Utils.getUploadUrl(Utils.attachmentUrlRegex,content.getContent());
		content.setHasFiles(true);
		if(imgUrlList.isEmpty())
			content.setHasImages(false);
		if(attachmentUrlList.isEmpty())
			content.setHasFiles(false);
		//更新文章
		cmsContentMapper.updateByPrimaryKeySelective(content);
		//向数据库插入图片地址
		if(content.getHasImages()) {
			CmsContentFile contentFile=new CmsContentFile();
			contentFile.setContentId(content.getId());
			contentFile.setIsImage(true);
			imgUrlList.forEach(url->{
				contentFile.setFilePath(url);
				cmsContentFileMapper.insertSelective(contentFile);
			});
		}
		//向数据库插入附件地址
		if(content.getHasFiles()) {
			CmsContentFile contentFile=new CmsContentFile();
			contentFile.setContentId(content.getId());
			contentFile.setIsImage(false);
			attachmentUrlList.forEach(url->{
				contentFile.setFilePath(url);
				cmsContentFileMapper.insertSelective(contentFile);
			});
		}
	}

	@Override
	public List<Article> getArticle(Integer pn, Integer pz) {
		CmsContentExample example=new CmsContentExample();
		//设置按发布日期排序
		example.setOrderByClause("publish_date DESC");
		PageHelper.startPage(pn,pz);
		List<Article> articleList=cmsContentMapper.selectByExampleWithBLOBsWithDepat(example);
		//将查询到的文章内容转换成纯文本,主页面上只显示标题，此处可不转。
		/*for(Article article:articleList){
			String htmlText=article.getContent().getContent();
			String text=Utils.html2Text(htmlText);
			article.getContent().setContent(text);
		}*/
		return articleList;
	}
}
